//
//  SPHomeViewController.m
//  ShopProject
//
//  Created by kuangbiao on 2018/11/26.
//  Copyright © 2018 KB. All rights reserved.
//

#import "SPHomeViewController.h"
#import "SPHomeCommandTableViewCell.h"
#import "SPHomeBestSellersTableViewCell.h"
#import "SPHomeCategoryView.h"
#import "SPProductCategoryModel.h"
#import "SPProductModel.h"
#import "YWRollView.h"
#import "UIButton+SPGraphicButton.h"

@interface SPHomeViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) YWRollView    *rollView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *bestSellersDataSource;    //热卖据源
@property (nonatomic, strong) NSMutableArray *categoryDataSource;       //分类数据源
@property (nonatomic, strong) NSMutableArray *dataSource;               //列表数据源

@end

@implementation SPHomeViewController

#pragma mark - lazy
- (NSMutableArray *)bestSellersDataSource {
    if (!_bestSellersDataSource) {
        _bestSellersDataSource = [[NSMutableArray alloc] init];
    }
    return _bestSellersDataSource;
}
- (NSMutableArray *)categoryDataSource {
    if (!_categoryDataSource) {
        _categoryDataSource = [[NSMutableArray alloc] init];
    }
    return _categoryDataSource;
}
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc] init];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"";

    [self createNav];
    [self createSubView];
    [self loadData];
}

- (void)loadData {
    SPProductModel *model = [[SPProductModel alloc] init];
    model.productId = @"1";
    model.productName = @"四羊方尊";
    model.productPrice = @"23";
    model.productSellerCount = @"1";
    model.productDesc = @"传统合艺坊";
    model.productIconUrl = @"banner";
    model.productAuctionCount = @"111111";
    
    SPProductCategoryModel *ca = [[SPProductCategoryModel alloc] init];
    ca.categoryId = @"1";
    ca.categoryName = @"推荐";
    
    [self.categoryDataSource addObjectsFromArray:@[ca, ca, ca, ca, ca, ca, ca]];
    [self.bestSellersDataSource addObjectsFromArray:@[model, model, model, model, model, model]];
    [self.dataSource addObjectsFromArray:@[model, model, model, model]];
    
    self.rollView.newsItems = self.dataSource;
    [self.rollView startRun];
}

- (void)createNav {
    UIButton *categoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [categoryButton setTitle:@"分类" forState:UIControlStateNormal];
    [categoryButton setImage:[UIImage imageNamed:@"czy_sb_sys_18x18_"] forState:UIControlStateNormal];
    categoryButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10];
    [categoryButton addTarget:self action:@selector(actionCategory) forControlEvents:UIControlEventTouchUpInside];
    [categoryButton setButtonContentCenter];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:categoryButton];
    UIButton *shoppingCarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [shoppingCarButton setTitle:@"购物车" forState:UIControlStateNormal];
    [shoppingCarButton setImage:[UIImage imageNamed:@"czy_sb_sys_18x18_"] forState:UIControlStateNormal];
    shoppingCarButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10];
    [shoppingCarButton addTarget:self action:@selector(actionShoppingCarButton) forControlEvents:UIControlEventTouchUpInside];
    [shoppingCarButton setButtonContentCenter];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:shoppingCarButton];
    UIButton *searchButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, WIDTH-100, 30)];
    searchButton.backgroundColor = RGBCOLOR(72, 72, 72);
    searchButton.layer.masksToBounds = YES;
    searchButton.layer.cornerRadius = 5;
    [searchButton setTitle:@"搜索你想要查找的物品" forState:UIControlStateNormal];
    [searchButton setImage:[UIImage imageNamed:@"czy_sb_sys_18x18_"] forState:UIControlStateNormal];
    [searchButton setImage:[UIImage imageNamed:@"czy_sb_sys_18x18_"] forState:UIControlStateHighlighted];
    searchButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10];
    [searchButton setTitleColor:RGBCOLOR(114, 114, 114) forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(actionSearchButton) forControlEvents:UIControlEventTouchUpInside];
    searchButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    searchButton.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    searchButton.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    self.navigationItem.titleView = searchButton;
}

- (void)createSubView {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, kScreenWidth*212/617.0)];
    self.rollView = [[YWRollView alloc] initWithFrame:headView.bounds];
    self.rollView.isHavePageVC = NO;
    [headView addSubview:self.rollView];
    self.rollView.isHavePageVC = NO;
    self.rollView.onClickItemBlock = ^(NSInteger index) {

    };
    self.tableView.tableHeaderView = headView;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView registerNib:[UINib nibWithNibName:@"SPHomeCommandTableViewCell" bundle:nil] forCellReuseIdentifier:@"SPHomeCommandTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"SPHomeBestSellersTableViewCell" bundle:nil] forCellReuseIdentifier:@"SPHomeBestSellersTableViewCell"];
}

#pragma mark - action
- (void)actionCategory {
    
}

- (void)actionShoppingCarButton {
    
}

- (void)actionSearchButton {
    
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return !section?1:self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!indexPath.section) {
        SPHomeBestSellersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SPHomeBestSellersTableViewCell"];
        cell.dataSource = self.bestSellersDataSource;
        cell.onSelectItem = ^(SPProductModel * _Nonnull product) {
            
        };
        
        return cell;
    }else {
        SPHomeCommandTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SPHomeCommandTableViewCell"];
        cell.product = self.dataSource[indexPath.row];
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return !indexPath.section?165:115;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return !section?55:35;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (!section) {
        UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 55)];
        headView.backgroundColor = [UIColor whiteColor];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:14];
        titleLabel.textColor = [UIColor colorWithHexString:@"333333"];
        titleLabel.text = @"即将结拍";
        [headView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(15);
            make.top.bottom.offset(0);
        }];
        
        return headView;
    }else {
        SPHomeCategoryView *headView = [[SPHomeCategoryView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 35)];
        headView.categorys = self.categoryDataSource;
        headView.onSelectCategory = ^(SPProductCategoryModel * _Nonnull category) {
            
        };
        
        return headView;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}


@end
